﻿layui.config({
    base: '/lib/notice/'
});
layui.use(['notice'], function () {
    var notice = layui.notice; // 允许别名 toastr

// 初始化配置，同一样式只需要配置一次，非必须初始化，有默认配置
    notice.options = {
        closeButton:true,//显示关闭按钮
        debug:false,//启用debug
        positionClass:"toast-top-right",//弹出的位置,
        newestOnTop: false,
        showDuration:"300",//显示的时间
        hideDuration:"1000",//消失的时间
        timeOut:"2000",//停留的时间
        extendedTimeOut:"1000",//控制时间
        showEasing:"swing",//显示时的动画缓冲方式
        hideEasing:"linear",//消失时的动画缓冲方式
        iconClass: 'toast-info', // 自定义图标，有内置，如不需要则传空 支持layui内置图标/自定义iconfont类名
        onclick: null, // 点击关闭回调
    };
});

//Ajax提交
function AjaxPost(type,Url,JsonData,LoadingFun,ReturnFun,async,processData,contentType) {
    $.ajax({
        type: type,
        url: Url,
        data: JSON.stringify(JsonData),
        dataType: 'json',
        headers: {
            'Authorization': localStorage.getItem('UT')
        },
        contentType: contentType,
        processData : processData, // 使数据不做处理
        async: async,
        beforeSend: LoadingFun,
        error: function (e) { AjaxErro(e); },
        success: function (res) {
                ReturnFun(res)
        }
    });
}

function syncPost(Url,JsonData,LoadingFun,ReturnFun) {
    AjaxPost('post',Url,JsonData,LoadingFun,ReturnFun,false,true,'application/json')
}

function asyncPost(Url,JsonData,LoadingFun,ReturnFun) {
    AjaxPost('post',Url,JsonData,LoadingFun,ReturnFun,true,true,'application/json')
}

function syncGet(Url,JsonData,LoadingFun,ReturnFun) {
    AjaxPost('get',Url,JsonData,LoadingFun,ReturnFun,false,true,'application/json')
}

function asyncGet(Url,JsonData,LoadingFun,ReturnFun) {
    AjaxPost('get',Url,JsonData,LoadingFun,ReturnFun,true,true,'application/json')
}

function syncDelete(Url,JsonData,LoadingFun,ReturnFun) {
    AjaxPost('delete',Url,JsonData,LoadingFun,ReturnFun,false,true,'application/json')
}




//Ajax 错误返回处理
function AjaxErro(e) {
        switch (e.status) {
            case 401:
                sessionStorage.setItem('401MSG',e.responseText)
                window.localStorage.removeItem("UT");
                window.localStorage.removeItem("userId");
                window.localStorage.removeItem("userName");
                top.location='login.html'
                break;
            default:
                ErroAlert(e.responseText);
        }
}




function Loading(msg) {
    layui.use(['notice'], function () {
        let notice = layui.notice;
        notice.info(msg)
    })
}

function success(msg) {
    layui.use(['notice'], function () {
        let notice = layui.notice;
        notice.success(msg)
    })
}

function error(msg) {
    layui.use(['notice'], function () {
        let notice = layui.notice;
        notice.error(msg)
    })
}

function warning(msg) {
    layui.use(['notice'], function () {
        let notice = layui.notice;
        notice.warning(msg)
    })
}

//弹出
function ErroAlert(e) {
    error(e)
}




function sleep (time) {
    return new Promise((resolve) => setTimeout(resolve, time));
}